package com.oildatacollection.rpctest.oilinfoget;

import com.rpc.OilInfoGet;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OilInfoImpl extends UnicastRemoteObject implements OilInfoGet {
    private final Map<String, String> wellMap;
    private static int cnt = 0;
    public OilInfoImpl() throws RemoteException {
        wellMap = new HashMap<>();
    }

    @Override
    public long getSummaryInterval() throws RemoteException {
        return 10;
    }

    @Override
    public List<String> getWellIds() throws RemoteException {
        List<String> ret = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            ret.add(Integer.toString(i));
        }
        return ret;
    }

    @Override
    public String getLastSummaryTime(String wellID) throws RemoteException {
        if (!wellMap.containsKey(wellID)) {
            return "1970-01-01T00:00:00.000Z";
        }
        return wellMap.get(wellID);
    }

    @Override
    public void sendSummaryResult(Map<String, Object> res) throws RemoteException {
        String latestTime = (String) res.get("latestTime");
        String wellID = (String) res.get("wellID");
        double rr = (double) res.get("sumFlow");
        wellMap.put(wellID, latestTime);
        System.out.println("idx=" + cnt + "; " + res + " wellID:" + wellID);
        cnt++;
    }
}
